<template>
  <div class="main">
    <el-row type="flex" justify="space-between" align="middle" class="bt-row">
      <h3>Model Series : </h3>
      <h3>{{this.name}}</h3>

      <div>
        <el-button type="primary" icon="el-icon-plus" @click="addClick">New</el-button>
        <el-button type="priamry" icon="el-icon-back" @click="backClick">Back</el-button>
      </div>
    </el-row>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="modelName" label="Model" width="220">
      </el-table-column>
      <el-table-column prop="productionName" label="Serie" width="220">
      </el-table-column>
      <!-- <el-table-column prop="imgurl" label="Image" width="240">
      </el-table-column> -->
      <el-table-column label="Datasheet" width="240">
        <template slot-scope="scope">
            <el-link type="primary" :href="scope.row.datasheeturl" target="_blank">{{scope.row.datasheeturl}}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="brief" label="Breif"> </el-table-column>
      <el-table-column prop="params" label="Params"> </el-table-column>

      <el-table-column fixed="right" label="OP" width="60">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="primary"  icon="el-icon-edit" circle size="small" style="display:block; margin:5px 3px"
            ></el-button
          >

          <el-button @click="deleteClick(scope.row)" type="danger" icon="el-icon-delete" circle size="small" style="display:block; margin:5px 3px"
            ></el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        @current-change="pageClick"
        >
      </el-pagination>
      
    </el-row>

    <el-dialog title="Model Editor" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="Series" :label-width="formLabelWidth">
          <el-input
            v-model="form.productionName"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="Model Name" :label-width="formLabelWidth">
          <el-input v-model="form.modelName" autocomplete="off"></el-input>
        </el-form-item>
        <el-input v-model="form.productionName" style="display: none" />
        <el-form-item label="Brief" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            rows="5"
            v-model="form.brief"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="Params" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            rows="12"
            v-model="form.params"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- <el-form-item label="Image" :label-width="formLabelWidth">
          <el-button @click="getImgFile" style="margin-top: 10px">
            <i class="el-icon-upload"></i>CLICK_UPLOAD
          </el-button>
          {{ this.form.imgUrl }}
          <input
            type="file"
            ref="imgFile"
            style="display: none"
            @change="handleImg($event)"
            utocomplete="off"
          />
        </el-form-item> -->

        <el-form-item label="DataSheet" :label-width="formLabelWidth">
          <el-button @click="getImgFile" style="margin-top: 10px">
            <i class="el-icon-upload"></i>CLICK_UPLOAD
          </el-button>
          {{ this.form.datasheeturl }}
          <input
            type="file"
            ref="imgFile"
            style="display: none"
            @change="handleImg($event)"
            utocomplete="off"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="loading">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="confirmClick">OK</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { getModels, saveModel, deleteModel } from "@/api/production";

export default {
  props: ["name"],
  data() {
    return {
      tableData: [],
      form: {
        id: "",
        modelName: "",
        productionName: this.name,
        brief: "",
        datasheeturl: "",
        sheetFile: null,
      },
      dialogFormVisible: false,
      formLabelWidth: "100px",
      loading: false,
      currentPage:1,
      total:0,
      pageSize:10,
    };
  },

  created() {
    // console.log("serier name=", this.name)
    this.getData();
  },

  methods: {
    getData() {
      getModels(this.currentPage, this.pageSize, this.name).then((response) => {
        // console.log(response);
        // var records = response.data.records.filter((d1) => {
        //   console.log(d1.productionName, this.name)
        //   return d1.productionName == this.name;
        // });
        this.tableData = response.data.records;

        this.currentPage = response.data.current;
        this.total = response.data.total
      });
    },

    handleClick(row) {
      this.dialogFormVisible = true;
      this.form = Object.assign({}, row);
      this.loading = false;
    },

    getImgFile() {
      this.$refs.imgFile.click();
    },

    handleImg(event) {
      event.preventDefault();
      let file = this.$refs.imgFile.files[0];
      this.$set(this.form, "datasheeturl", file.name);
      this.form.sheetFile = file;
      console.log("select file ", file);
    },

    confirmClick() {
      this.loading = true;
      saveModel(this.form).then((response) => {
        console.log(response);
        this.loading = false;
        this.dialogFormVisible = false;
        this.$message.success("save OK");
        this.getData();
      });
    },
    addClick() {
      console.log();
      this.handleClick({ productionName: this.name });
    },
    deleteClick(row) {
      this.$confirm("delete Model?", "hint", {
        confirmButtonText: "OK",
        cancelButtonText: "cancel",
        type: "warning",
      }).then(() => {
        deleteModel(row.id).then((res1) => {
          this.$message({
            type: "success",
            message: "delete record!",
          });
          this.getData();
        });
      });
    },

    backClick() {
      this.$router.replace({ path: "/production/product" });
    },
    pageClick(){
      this.getData()
    }
  },
};
</script>
  
  <style lang="scss">
.main {
  padding: 8px;
}
.bt-row {
  padding: 8px;
}
</style>